    
module.exports = function(){
    // /**
    //  * 临时代码g，苟苟苟控制器Controller
    //  */

    /**
        * 房间搜索
        */

    //获取拥有控制权限的房间
    for (roomName in Game.rooms) {

        RoomSourcesTargets = Game.rooms[roomName].find(FIND_SOURCES);
        RoomController = Game.rooms[roomName].controller;
    }


    let CreepUpgrade = function (creep) {

        //判断自己的状态，0:采集状态,1升级状态，刚出生的默认为0

        //采集状态，资源位置:9,15
        //获取target

        if (creep.memory.state == 0) {

            let target = RoomSourcesTargets[0];
            //console.log(JSON.stringify(target.pos));
            if (creep.harvest(target) == -9) {
                creep.moveTo(target);
                creep.say("距离不够");
            }

        }

        if (creep.memory.state == 1) {
            let target = RoomController;
            if (creep.upgradeController(target) == -9) {
                //console.log(Game.cpu.getUsed());
                //creep.moveTo(target);
                //console.log(Game.cpu.getUsed());
                creep.moveTo(target);
                //console.log(Game.cpu.getUsed());
            }
        }
        


        if (creep.store.getUsedCapacity(RESOURCE_ENERGY) == 0) {
            //没有任何能量修改为采集状态
            creep.memory.state = 0;
            creep.say("Harvest");
        }


        if (creep.store.getFreeCapacity(RESOURCE_ENERGY) == 0) {
            //能量已经储存满 修改状态为升级状态
            creep.memory.state = 1;
            creep.say("Upgrade");
        }

        //console.log(creep.store.getUsedCapacity(RESOURCE_ENERGY))

    }


    //判断是否有Creep
    let tempHas = false;

    for (creep in Game.creeps) {
        let creepo = Game.creeps[creep]; 
        //console.log(creepo.pos.x+","+creepo.pos.y);
        if (Game.creeps[creep].memory.type == "Upgrade") {
            CreepUpgrade(Game.creeps[creep]);
            tempHas = true;
            break;
        }
    }

    //如果没有升级的Creep
    if (!tempHas) {
        //生成
        Game.spawns['Spawn1'].spawnCreep(
            [WORK, CARRY, MOVE],
            Game.time % 999,
            {
                memory: {
                    type: "Upgrade",
                    state: 0
                }
            }
        );
    }


    //清除不用的Creep的Memory
    for (item in Memory.creeps) {
        if (Game.creeps[item] == undefined) {
            delete Memory.creeps[item];
        }
    }
}